package com.wss.lsl.alading.controller.unit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wss.lsl.alading.domain.Unit;
import com.wss.lsl.alading.service.IUnitService;

@Controller
@RequestMapping("/unit")
public class UnitController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private IUnitService unitService;

	@RequestMapping("/show")
	@ResponseBody
	public String show(@ModelAttribute Unit unit, Model model) {

		logger.info("pointId={}, pointName={}, pointRate={}", new Object[] {
				unit.getPointId(), unit.getPointName(), unit.getPointRate() });

		int type = 3; // 保存成功
		try {
			unitService.createOrUpdateUnit(unit);
		} catch (Exception e) {
			type = 4; // failure
		}

		return "{\"type\":" + type + "}";
	}

	@RequestMapping("/showUnit")
	public String showUnit(Model model) {

		model.addAttribute("unit", unitService.findUnit());
		return "unit/unitShow";
	}
}
